

package com.teaching.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaching.common.Result;

import com.teaching.dto.SubjectDTO;
import com.teaching.entity.Subject;
import com.teaching.mapper.SubjectMapper;
import com.teaching.service.SubjectService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 论坛主题表
 *
 * @author xubb
 * @date 2021-03-22 15:29:17
 */
@RestController
@RequestMapping("/subject")
@Api(value = "subject", tags = "论坛主题表管理")
public class SubjectController {


    @Resource
    private SubjectService subjectService;

    @Resource
    private SubjectMapper subjectMapper;

    /**
     * 分页查询
     *
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/getSubjectList")
    public Result<IPage<Subject>> getSubjectList(@RequestBody SubjectDTO dto) {
        Page<Subject> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        Subject subject = new Subject();
        BeanUtil.copyProperties(dto, subject);
        return Result.success(subjectMapper.getSubjectList(page, subject));
    }


    /**
     * 通过id查询
     *
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @PostMapping("/getSubjectInfo")
    public Result getSubjectInfo(@RequestBody Subject subject) {

        return Result.success(subjectService.getById(subject.getId()));
    }

    /**
     * 新增
     *
     * @param subject
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody Subject subject) {

        return Result.success(subjectService.save(subject));
    }

    /**
     * 修改
     *
     * @param subject
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping("/updateById")
    public Result updateById(@RequestBody Subject subject) {

        return Result.success(subjectService.updateById(subject));
    }

    /**
     * 通过id删除
     *
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @PostMapping("/removeById")
    public Result removeById(@RequestBody Subject subject) {
        return Result.success(subjectService.removeById(subject.getId()));
    }

}
